{% extends 'base.html' %}

{% block title %}小区话题{% endblock %}
{% block stylesheet %}
<style type="text/css">
.weui-tab__bd {
    box-sizing: border-box;
    height: 90%;
}
</style>

{% endblock %}
{% block content %}
  	<div class="weui-tab">
		<div class="weui-navbar">
		  <div class="weui-navbar__item weui_bar__item_on">

		  	<div class="weui-flex">
  		      <div class="weui-flex__item"><div class="placeholder">{{g.user.community.cmnt_name}}-小区话题</div></div>
<!-- 		      <div class="weui-flex__item"><div class="placeholder"></div></div> -->
		      <!-- <div class="weui-flex__item"><div class="placeholder"></div></div> -->
		      <div class="weui-flex__item"><div class="placeholder"><a href="/newTopic" class="weui-btn weui-btn_mini weui-btn_primary">发表</a></div></div>
		    </div>

		  </div>
		</div>

      <div class="weui-tab__bd">
        <div id="tab1" class="weui-tab__bd-item weui-tab__bd-item--active">

          <div class="weui-panel">
	        <!-- <div class="weui-panel__hd">小区话题</div> -->
	        <div class="weui-panel__bd">
           {% if topics %}
  	         {% for topic in topics %}
  	         
                <div class="weui-media-box weui-media-box_text" data-tpcid="{{topic['id']}}">
  	            <h4 class="weui-media-box__title">{% if '00' == topic['category'] %}<i class="weui-icon-info-circle"></i>
                {% elif '01' == topic['category'] %}
                <i class="weui-icon-info"></i>[公告]
                 {% endif %}{{topic['title']}}</h4>
  	            <p class="weui-media-box__desc">{{topic['content']|striptags}}</p>
  	            <ul class="weui-media-box__info">
  	              <li class="weui-media-box__info__meta">{{topic['user'].nickname}}</li>
  	              <li class="weui-media-box__info__meta">{{topic['pub_time'].strftime('%Y-%m-%d %H:%M:%S')}}</li>
  	              <li class="weui-media-box__info__meta weui-media-box__info__meta_extra">
  	              	{% if '00' == topic['category'] %}话题{% elif '01' == topic['category'] %}公告 {% else %}其他 {% endif %}
  	              </li>
  	            </ul>
  	          </div>
  	         {% endfor %}

           {% else %}
              <div class="weui-loadmore weui-loadmore_line">
                <span class="weui-loadmore__tips">暂无数据</span>
              </div>
           {% endif %}

	        </div>
 {% if topics %}
         <div class="weui-panel__ft">
          <a href="javascript:loadmore();" class="weui-cell weui-cell_access weui-cell_link">
            <div class="weui-cell__bd">查看更多</div>
            <span class="weui-cell__ft"></span>
          </a>    
         </div>
  {% endif %}
	      </div>

        </div>
      </div>

      <div class="weui-tabbar">
        <a href="#" class="weui-tabbar__item weui-bar__item--on">
          <!-- <span class="weui-badge" style="position: absolute;top: -.4em;right: 1em;">8</span> -->
          <div class="weui-tabbar__icon">
            <img src="{{ url_for('static', filename='images/icon_nav_dialog.png') }}" alt="">
          </div>
          <p class="weui-tabbar__label">话题</p>
        </a>
        <a href="/repairs" class="weui-tabbar__item">
          <div class="weui-tabbar__icon">
            <img src="{{ url_for('static', filename='images/icon_nav_article.png') }}" alt="">
          </div>
          <p class="weui-tabbar__label">报修</p>
        </a>
        <a href="/discovery" class="weui-tabbar__item">
          <div class="weui-tabbar__icon">
            <img src="{{ url_for('static', filename='images/icon_nav_button.png') }}" alt="">
          </div>
          <p class="weui-tabbar__label">发现</p>
        </a>
        <a href="/auth/profile" class="weui-tabbar__item">
          <div class="weui-tabbar__icon">
            <img src="{{ url_for('static', filename='images/icon_nav_cell.png') }}" alt="">
          </div>
          <p class="weui-tabbar__label">我</p>
        </a>
      </div>
    </div>



{% endblock %}

{% block jsblock %}

//$(".weui-media-box.weui-media-box_text").on('click', function(){
//   //alert($(this).data('tpcid'))
//   $(location).attr('href', '/showTopic/'+$(this).data('tpcid'));
//});


$(".weui-panel__bd").delegate(".weui-media-box.weui-media-box_text","click",function(){
     $(location).attr('href', '/showTopic/'+$(this).data('tpcid'));
});

function comingsoon() {
  $.toast("功能实现中...<br>敬请期待～", "text");
}

//$(".weui-loadmore").hide();

// var loading = false;
//  $("#tab1").infinite().on("infinite", function() {
//  alert()
//    if(loading) return;
//    loading = true;
//    //$(".weui-loadmore").show();
//    setTimeout(function() {
//      alert()
//    //  $(".weui-loadmore").hide();
//      loading = false;
//    }, 2000);
//  });

var page = 1;

function loadmore() {
    page++;

    $.ajax({
    type: 'GET',
    url: '/loadTopics/'+page,
    success: function(data) {
          divHtml="";
           $.each(data['topics'], function (n, topic) {
            var cate = "";
            var icon = "";
            if ('00' == topic['category']) {
              cate = '话题';
              icon = '<i class="weui-icon-info-circle"></i>'
            }else if ('01' == topic['category']) {
              cate = '公告';
              icon = '<i class="weui-icon-info"></i>[公告]'
            }else {
              cate = '其他';
              icon = '<i class="weui-icon-info-circle"></i>'
            }
              divHtml = divHtml+ '<div class="weui-media-box weui-media-box_text" data-tpcid="'
                               + topic['id'] + '">'
                               + '<h4 class="weui-media-box__title">' + icon
                               + topic['title'] + '</h4>'
                               + '<p class="weui-media-box__desc">' + topic['content'] + '</p>'
                               + '<ul class="weui-media-box__info">'
                               + '<li class="weui-media-box__info__meta">' + topic['nickname'] + '</li>'
                               + '<li class="weui-media-box__info__meta">' + topic['pub_time'] + '</li>'
                               + '<li class="weui-media-box__info__meta weui-media-box__info__meta_extra">'
                               + cate + '</li></ul></div>';

           });

           if(data['topics'].length < 5 ) {
            divHtml = divHtml+'<div class="weui-loadmore weui-loadmore_line"> <span class="weui-loadmore__tips">没有更多数据</span> </div>'

             $(".weui-panel__ft").hide();
            }

           $(".weui-panel__bd").append(divHtml);

             },
    error: function(msg) {
          $.toast("获取失败请重试", "forbidden");
          page--;
      }
  });

}

{% endblock %}